<template>
    <TableView class="table">
        <template #form>
            <NSpace justify="space-between">
                <NSpace align="center">
                    类目名称:
                    <NInput style="width: 200rem" placeholder="请输入类目名称" v-model:value="paginationReactive.categoriesName"
                        clearable />
                    <NButton type="primary" style="width: 78rem" ghost
                        @click="() => ((paginationReactive.page = 1), getAssetsTypeList())">
                        查询
                    </NButton>
                </NSpace>
                <NButton type="primary" style="width: 115rem"
                    @click="$router.push({ path: '/OA/AssetsManagement/AssetsType/AssetsTypeAdd' })">
                    新建类目
                </NButton>
            </NSpace>
        </template>

        <template #table="props">
            <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
                :loading="tableLoading" children-key="childList" :expanded-row-keys="expandRowKey" :on-update:expanded-row-keys="handleExpandRow" :row-key="(e) => e.id" />
        </template>

        <template #pagination>
            <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
                :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
                @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
                show-size-picker show-quick-jumper :suffix="() => '页'">
                <template #prefix>
                    <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
                </template>
            </NPagination>
        </template>
    </TableView>

    <NModal :auto-focus="false" v-model:show="modalVisible" @close="modalVisible = false">
        <VDialogContent @close="modalVisible = false" @submit="AssetsTypeDelete" title="删除提示">
            <div style="width: 300rem; height: 100rem; text-align: center; display: flex;justify-content: center;align-items: center;">
                您确定删除{{ typeInfo.categoriesName }}吗？
            </div>
        </VDialogContent>
    </NModal>
</template>
<script setup lang="ts">
import { NSpace, NButton, NDataTable, NPagination, useMessage, NModal, NInput } from 'naive-ui'
import { h, reactive, ref, watch, onDeactivated } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import type { DataTableColumns } from 'naive-ui'
import TableView from '@/components/TableView.vue'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import router from '@/router'
import { useViewerStore } from '@/views/OA/Common/store'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()
const modalVisible = ref(false)

const expandRowKey = ref([])
const handleExpandRow = (v: any) => {
    // console.log(v)
    expandRowKey.value = v
}
const getExpandRows = () => {
    let result: any[] = []
    const expand = (data: any) => {
        data.forEach((t: any) => {
            result.push(t.id)
            if(t.childList && t.childList?.length > 0) {
                expand(t.childList)
            }
        })
    }
    expand(tableData.value)
    handleExpandRow(result)
}
const columns: DataTableColumns = [
    {
        title: '类目名称',
        key: 'categoriesName',
        width: 100,
        align: 'left'
    },
    {
        title: '上级',
        key: 'parentName',
        width: 60,
        align: useStore.isCenter
    },
    {
        title: '备注',
        key: 'remark',
        width: 60,
        align: useStore.isCenter,
        ellipsis: {
            tooltip: {
                contentStyle: {
                    maxWidth: '400rem'
                }
            }
        }
    },
    {
        title: '类型代码',
        key: 'categoriesCode',
        width: 60,
        align: useStore.isCenter,
        ellipsis: {
            tooltip: {
                contentStyle: {
                    maxWidth: '400rem'
                }
            }
        }
    },
    {
        title: '创建时间',
        key: 'createTime',
        width: 60,
        align: useStore.isCenter
    },
    {
        title: '创建人',
        key: 'createBy',
        width: 60,
        align: useStore.isCenter
    },
    {
        title: '操作',
        key: 'action',
        align: useStore.isCenter,
        width: 60,
        fixed: 'right',
        render: (row: any) => [
            h(
                NButton,
                {
                    type: 'primary',
                    color: useStore.color.delete,
                    quaternary: true,
                    onClick: async () => {
                        typeInfo.value = row
                        modalVisible.value = true
                    }
                },
                {
                    default: () => '删除'
                }
            ),
            h(
                NButton,
                {
                    type: 'primary',
                    quaternary: true,
                    onClick: async () =>
                        router.push({ path: '/OA/AssetsManagement/AssetsType/AssetsTypeUpdate', query: { id: row.id } })
                },
                {
                    default: () => '编辑'
                }
            )
        ]
    }
]
// 上一页下一页
const paginationReactive = reactive({
    page: 1,
    pageSize: 10,
    pageSizes: [10, 20, 50, 100, 500],
    itemCount: 0,
    categoriesName: null, //类目名称
    onChange: (page: number) => {
        paginationReactive.page = page
        getAssetsTypeList()
    },
    onUpdatePageSize: (pageSize: number) => {
        paginationReactive.pageSize = pageSize
        paginationReactive.page = 1
        getAssetsTypeList()
    }
})
// 删除
const typeInfo = ref()
const AssetsTypeDelete = async () => {
    modalVisible.value = false
    tableLoading.value = true
    const [error, res] = await sendRequest({
        module: 'assetsManagement',
        api: 'assetsTypeDelete',
        override: {
            data: [typeInfo.value.id]
        }
    })
    if (error) return message.error('删除失败'), (tableLoading.value = false)
    if (res.code !== 0) return message.error('删除失败'), (tableLoading.value = false)
    getAssetsTypeList()
    message.success('删除成功')
}
// 获取列表
const tableData: any = ref([])
const tableLoading = ref(false)
const getAssetsTypeList = async () => {
    tableLoading.value = true
    const [error, res] = await sendRequest({
        module: 'assetsManagement',
        api: 'assetsTypeList',
        override: {
            params: {
                page: paginationReactive.page,
                limit: paginationReactive.pageSize,
                categoriesName: paginationReactive.categoriesName
            }
        }
    })
    tableLoading.value = false
    if (error) return message.error(error.message)
    if (res.code !== 0) return message.error(res.msg)
    tableData.value = []
    setTimeout(() => {
        tableData.value = res.data.list || []
        paginationReactive.itemCount = res.data.totalCount
        handleExpandRow([])
        if(paginationReactive.categoriesName && paginationReactive.categoriesName !== "") {
            getExpandRows()
        }
    }, 0)
}

const keyUpDown = (e: any) => e.key == 'Enter' && getAssetsTypeList()
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
    () => route,
    async () => {
        if (route.path == '/OA/AssetsManegement/AssetsType/AssetsType') {
            getAssetsTypeList()
            window.addEventListener('keyup', keyUpDown, false)  // 回车
        }
    },
    { deep: true, immediate: true }
)

</script>
<style scoped lang="scss">
.table {
    padding: 29rem 30rem 42rem;
}
</style>
      